const renderNums = function(num,unit){
    num += '';
    var numIndex = num.indexOf('.');
    var intStr = '',pointStr = '',floatStr = '',unitStr='';
    var intArr,floatArr;
    intArr = (num.slice(0,numIndex)).split('');
    intArr.map((item)=>{
        intStr += '<div class="num_item num">'+item+'</div>'
    })
    if(numIndex > -1){
        floatArr = (num.slice(numIndex+1)).split('')
        pointStr = '<div class="num_item point">.</div>';
        floatArr.map((item)=>{
            floatStr += '<div class="num_item num">'+item+'</div>'
        })
    }else{
        pointStr = '';
        floatStr = ''
    }
    unitStr = unit?'<div class="num_item unit">'+unit+'</div>':''
    // $(id).html(intStr + pointStr + floatStr)
    return intStr + pointStr + floatStr + unitStr

}
const dateFormat = function(dateStr,split,isTime){
	split = split||'-';
	if(dateStr){
		var date = new Date(dateStr);
	    var y = date.getFullYear();      
	    var m = date.getMonth() + 1;      
	    m = m < 10 ? ('0' + m) : m;      
	    var d = date.getDate();      
	    d = d < 10 ? ('0' + d) : d;      
	    var h = date.getHours();    
	    h = h < 10 ? ('0' + h) : h;    
	    var minute = date.getMinutes();    
	    var second = date.getSeconds();    
	    minute = minute < 10 ? ('0' + minute) : minute;      
		second = second < 10 ? ('0' + second) : second;   
		if(isTime){
			return y + split + m + split + d+' '+h+':'+minute+':'+second;    
		}else{
			return y + split + m + split + d;    
		}
	    
	}else{
		return dateStr
  }
}
const adaptePX = function (px){
    var vHeight = window.screen.height;
    // console.log(window.height , document.documentElement.clientHeight , document.body.clientHeight,window.screen.height)
    return px * vHeight / 1080;
}
const formatPercent = function(num){
    num -= 0;
    let percent = 0,total = 0;
    if(Math.abs(num)<1 && Math.abs(num)>0){
        total = 1;
        percent = (num*100).toFixed(2);
    }else if(Math.abs(num)<100 && Math.abs(num)>1){
        total = 100;
        percent = (num).toFixed(2);
    }else if(Math.abs(num)==1 || Math.abs(num)==100){
        percent = 100
    }
    return percent
}
const getColor = function (barColor,index, first,type,orient) {
    type = type || 'linear';
    orient = orient || 'y';
    var barColor = barColor || [];
    if (barColor[index]) {
      var color1 = barColor[index].color1;
      var color2 = barColor[index].color2;
      var postion = (barColor[index].postion || 1) ;
      if (first) return color1;
      if (color2) {
        if(type == 'linear'){
          let orientPosition = {
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
          }
          if(orient == 'x'){
            orientPosition = {
              x: 0,
              y: 0,
              x2: 1,
              y2: 0,
            }
          }
          return Object.assign(orientPosition,{
            type: 'linear',
            colorStops: [{
              offset: 0,
              color: color1 // 0% 处的颜色
            }, {
              offset: postion,
              color: color2 // 100% 处的颜色
            }],
            global: false // 缺省为 false
          });
        }else if(type == 'radial'){
          return {
            type: 'radial',
            x: 0.5,
            y: 0.5,
            r: 0.5,
            colorStops: [{
                offset: 0, color: color2 // 0% 处的颜色
            }, {
                offset: 1, color: color1 // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        }
      }
      return color1;
    }
  }
export {renderNums,adaptePX,formatPercent,getColor,dateFormat};